add API for managing palette based formats
authorØyvind Kolås <pippin@gimp.org>
Sat, 17 Mar 2012 12:22:10 +0000 (12:22 +0000)
committerØyvind Kolås <pippin@gimp.org>
Sat, 17 Mar 2012 12:22:10 +0000 (12:22 +0000)
babl/Makefile.am
babl/babl-ids.h
babl/babl-types.h
babl/babl.h
babl/base/babl-base.h
tests/Makefile.am
tests/palette.c [new file with mode: 0644]

index 93c2657f83f56e10cdb21904f39597fd141eeb1b..cefab57238d77336e24aef7c212d4ea312fb81cf 100644 (file)
@@ -27,6 +27,7 @@ c_sources =                           \
        babl-memory.c                   \
        babl-model.c                    \
        babl-mutex.c                    \
+       babl-palette.c    \
        babl-sampling.c                 \
        babl-sanity.c                   \
        babl-type.c                     \
index f0f6c6198cbb1ad5b6972989d1e269b37bb7e4b3..f26add865216f6cf871164b0520c678350f49bb2 100644 (file)
@@ -85,6 +85,7 @@ enum {
   BABL_CB,
   BABL_CR,
   BABL_PADDING,
+  BABL_PAL,
   BABL_COMPONENT_LAST_INTERNAL,
 
   BABL_FORMAT_BASE = 100000,
index 7871e3b78d26536dd729de905547df91d7c6e2d8..ea30b7f5671df48ec15e57ce70fe4bd656b08e89 100644 (file)
@@ -35,7 +35,9 @@ typedef union _Babl Babl;
  */
 typedef long (*BablFuncLinear)    (const char  *src,
                                    char  *dst,
-                                   long   n);
+                                   long   n,
+                                   void  *src_model_data,
+                                   void  *dst_model_data);
 
 /* TypePlanar,ModelPlanar and FormatPlanar */
 typedef long (*BablFuncPlanar)    (int    src_bands,
@@ -44,6 +46,8 @@ typedef long (*BablFuncPlanar)    (int    src_bands,
                                    int    dst_bands,
                                    char  *dst[],
                                    int    dst_pitch[],
-                                   long   n);
+                                   long   n,
+                                   void  *src_model_data,
+                                   void  *dst_model_data);
 
 #endif
index 49110d37a9af7904cc1df356b4b6263851a24b53..e4b00829b9c4e97753cca12cafb8b7fe0465af73 100644 (file)
@@ -200,6 +200,27 @@ int babl_format_is_format_n (Babl *format);
 Babl * babl_conversion_new (void *first_arg,
                             ...) BABL_ARG_NULL_TERMINATED;
 
+/**
+ * create a new palette based format, name is optional pass in NULL to get
+ * an anonymous format.
+ */
+Babl *babl_new_palette         (const char *name);
+
+/**
+ * Assign a palette to a palette format, the data is a single span of pixels
+ * representing the colors of the palette.
+ */
+void  babl_palette_set_palette (Babl              *babl,
+                                Babl              *format,
+                                void              *data,
+                                int                count);
+
+/**
+ * reset a palette to initial state.
+ */
+void  babl_palette_reset       (Babl              *babl);
+
+
 
 /*
  * Backwards compatibility stuff
index 57d4cebafd1da73a6ebe3d6763a5dbb8149dfb28..bed3c64473c2b2540d481539e3f62116d5d645c9 100644 (file)
@@ -28,6 +28,7 @@ void babl_base_type_u8     (void);
 void babl_base_type_u16    (void);
 void babl_base_type_u32    (void);
 
+void babl_base_model_pal   (void);
 void babl_base_model_rgb   (void);
 void babl_base_model_gray  (void);
 void babl_base_model_ycbcr (void);
index 8b9060d4eb840284790ed4f00d84810f39744d26..ba88a9c58c7ecde3784b83a8224ed687ec0f9782 100644 (file)
@@ -10,6 +10,7 @@ TESTS =                               \
        sanity                  \
        babl_class_name         \
        types                   \
+       palette \
        extract \
        nop \
        n_components            \
@@ -33,6 +34,4 @@ noinst_PROGRAMS =             \
        babl-html-dump          \
        conversions             \
        formats                 \
-       extract \
-       nop                     \
        $(TESTS)
diff --git a/tests/palette.c b/tests/palette.c
new file mode 100644 (file)
index 0000000..02f79ac
--- /dev/null
@@ -0,0 +1,108 @@
+/* babl - dynamically extendable universal pixel conversion library.
+ * Copyright (C) 2005, Øyvind Kolås.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <assert.h>
+#include "babl.h"
+
+
+#define CHECK_CONV(test_name, componenttype, src_fmt, dst_fmt, src_pix, expected_pix) \
+  {       \
+  Babl *fish;       \
+  int i;       \
+  fish = babl_fish (src_fmt, dst_fmt);       \
+  if (!fish)       \
+    {       \
+      printf ("  %s failed to make fish\n", test_name);       \
+      OK = 0;       \
+    }       \
+  for (i = 0; i < sizeof(src_pix)/sizeof(src_pix[0]); i ++)       \
+    {       \
+      int c;\
+      componenttype result[10];       \
+      babl_process (fish, src_pix[i], result, 1);       \
+      for (c = 0; c < sizeof(expected_pix[i])/sizeof(expected_pix[i][0]); c++) \
+      if (result[c] != expected_pix[i][c])       \
+        {       \
+          printf (" %s failed #%i[%i]  got %i expected %i\n", test_name, i, c, result[c], expected_pix[i][c]);       \
+          OK = 0;          \
+        }       \
+    }       \
+  }
+
+
+int
+main (int    argc,
+      char **argv)
+{
+  int OK = 1;
+  babl_init ();
+
+  {
+    unsigned char in[][1]   = {{        0},{          1},{          2},{15}};
+    unsigned char out[][4]  = {{0,0,0,255},{127,0,0,255},{0,127,0,255},{255,255,255,255}};
+    Babl *palA = babl_new_palette (NULL);
+    Babl *palB = babl_new_palette (NULL);
+
+    CHECK_CONV("pal to rgba", unsigned char,
+        palA, babl_format("RGBA u8"),
+        in, out);
+
+    CHECK_CONV("pal to rgba", unsigned char,
+        palB, babl_format("RGBA u8"),
+        in, out);
+
+    CHECK_CONV("pal to rgba", unsigned char,
+        palA, babl_format("RGBA u8"),
+        in, out);
+  }
+
+  {
+    unsigned char in[][4]  = {{0,0,0,255},{140,0,0,255},{0,127,0,255}};
+    unsigned char out[][1] = {{        0},{          1},{          2}};
+
+    CHECK_CONV("rgba to pal", unsigned char,
+         babl_format("RGBA u8"), babl_new_palette ("palC"),
+         in, out);
+  }
+
+  /* check with a custom floating point palette */
+  {
+    float palette[] = {
+      0.5,  1.0,
+      0.23, 0.42,
+      1.0,  0.2
+    };
+
+    unsigned char in[][1]   = {{         0},{          1},{          2}};
+    unsigned char out[][4]  = {{128,128,128,255},{59,59,59,107},{255,255,255,51}};
+
+    Babl *pal = babl_new_palette (NULL);
+
+    babl_palette_set_palette (pal, babl_format ("YA float"), palette, 3);
+
+    CHECK_CONV("rgba to YA float pal", unsigned char,
+         pal, babl_format("RGBA u8"),
+         in, out);
+  }
+
+  babl_exit ();
+  return !OK;
+}